package com.front.web.lvyouFood;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.bootdo.common.redisUtils.JedisUtils;
import com.bootdo.common.utils.EmailUtils;
import com.bootdo.common.utils.Hog;
import com.bootdo.common.utils.R;
import com.bootdo.lvyou.domain.LvyouMember;
import com.bootdo.lvyou.service.LvyouMemberService;
import com.front.web.commons.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/f/")
public class SendMobileMessageController {

    public static final String CODEKEY="SMS_159550398";

    @Autowired
    private LvyouMemberService lvyouMemberService;
    /**
     * 发送短信验证码的接口
     * http://127.0.0.1/f/sendmessage/15230060935
     * @param mobile
     * @return
     */
    @RequestMapping("/sendmessage/{mobile}")
    public R sendMessage(@PathVariable String mobile){
        LvyouMember mobileMember = lvyouMemberService.getByMobile(mobile);
        if(mobileMember!=null){
            return R.error("手机号码已存在");
        }
        if(JedisUtils.exists(CODEKEY+mobile+CODEKEY)){
            return  R.error("60秒之后才可以再次发送");
        }
        String code = EmailUtils.getRandomStr(4);
        Hog.e("{}短信验证码：{}",mobile,code);
        SendSmsResponse smsresponse =   SendSms.sendSms(mobile,code,"送吧到店", CODEKEY);
        JedisUtils.set(CODEKEY+mobile, code, 60*10);
        JedisUtils.set(CODEKEY+mobile+CODEKEY, code, 60);
        if("OK".equals(smsresponse.getCode())){
            return  R.ok("验证码发送成功");
        }else {
            return  R.error(smsresponse.getMessage());
        }
    }
}
